package com.rocksdata.mc.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.ctz.commonframe.ui.BaseActivity;
import com.ctz.commonframe.utils.SharedPrefrencesUtil;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.rocksdata.mc.R;
import com.rocksdata.mc.SharedPreferencesKey;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;


/**
 * Created by 陈堂正 on 2017/5/9.
 */
@ContentView(R.layout.activity_guide)
public class GuideActivity extends BaseActivity {
    @ViewInject(R.id.rpv_guide)
    private RollPagerView rpvGuide;
    @ViewInject(R.id.tv_enter_app)
    private TextView tvEnterApp;

    private ColorPointHintView pointHintView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int[] guideResIds = {R.mipmap.image_guide1, R.mipmap.image_guide2, R.mipmap.image_guide3};
        pointHintView = new ColorPointHintView(this, Color.BLACK, 0xFFE4E4E4);
        rpvGuide.setHintView(pointHintView);
        final GuideListAdapter adapter = new GuideListAdapter(this, guideResIds);
        rpvGuide.setAdapter(adapter);
        rpvGuide.getViewPager().setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                rpvGuide.onPageSelected(position);
                if(position == adapter.getCount() - 1){
                    tvEnterApp.setVisibility(View.VISIBLE);
                    rpvGuide.setHintView(null);
                }else{
                    tvEnterApp.setVisibility(View.GONE);
                    rpvGuide.setHintView(pointHintView);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Event(value = R.id.tv_enter_app)
    private void onClick(View view){
        switch (view.getId()) {
            case R.id.tv_enter_app:
                SharedPrefrencesUtil.put(SharedPreferencesKey.KEY_FIRST_ENTER, false);
                startActivity(new Intent(GuideActivity.this, LoginActivity.class));
                finish();
                break;
        }
    }

    public class GuideListAdapter extends StaticPagerAdapter {
        private int[] resIds;
        private Context context;

        public GuideListAdapter(Context context, int[] resIds) {
            this.context = context;
            this.resIds = resIds;
        }

        @Override
        public int getCount() {
            return resIds.length;
        }

        @Override
        public View getView(ViewGroup container, int position) {
            ImageView view = new ImageView(context);
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
            view.setImageResource(resIds[position]);
            return view;
        }
    }
}